package com.westa.smallshop.util;

import android.content.Context;

import com.westa.smallshop.data.User;


/**
 * Created by dune on 15/12/23.
 */
public class UserSPUtils {

    public final static String FILE_NAME = "user_info";

    public static void saveUserToSp(User user, Context context) {

        setAccount(context, user.getAccount());
        setUserName(context, user.getUsername());
        setPortrait(context, user.getPortrait());

    }

//    public User(long id, String username, String account, String password, List<String> goods_list, String portrait) {
//        this.id = id;
//        this.username = username;
//        this.account = account;
//        this.password = password;
//        this.goods_list = goods_list;
//        this.portrait = portrait;
//    }

    public static User getUserFromSp(Context context) {

//        return new User(getId(context), getRealName(context), getPhoneNumber(context), getRemarks(context),
//                getPortrait(context), getToken(context), getRole(context), getUuid(context));
        User user = new User();
        user.setAccount(getAccount(context));
        return user;

    }

    public static String getAccount(Context context) {

        return (String) getUserInfo(context, "account", "");
    }

    public static void setAccount(Context context, String account) {

        putUserInfo(context, "account", account);
    }

    public static String getUserName(Context context) {

        return (String) getUserInfo(context, "username", "");
    }

    public static void setUserName(Context context, String username) {

        putUserInfo(context, "username", username);
    }

    public static String getPortrait(Context context) {

        return (String) getUserInfo(context, "portrait", "");
    }

    public static void setPortrait(Context context, String portrait) {

        putUserInfo(context, "portrait", portrait);
    }

    public static Object getUserInfo(Context context, String key, Object defaultObject) {

        return SPUtils.get(context, key, defaultObject, UserSPUtils.FILE_NAME);
    }

    public static void putUserInfo(Context context, String key, Object value) {

        if (value != null) {
            SPUtils.put(context, key, value, UserSPUtils.FILE_NAME);
        }
    }

    public static void removeUserInfo(Context context, String key) {

        SPUtils.remove(context, key, UserSPUtils.FILE_NAME);
    }

    public static void clear(Context context) {

        SPUtils.clear(context, FILE_NAME);
    }


}
